package net.maku.module.controller;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import net.maku.framework.common.utils.Result;
import net.maku.module.entity.AgKeywordConfig;
import net.maku.module.service.AgKeywordConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/keywordConfig")
public class AgKeywordConfigController {

    @Autowired
    private AgKeywordConfigService keywordConfigService;

    @Operation(summary = "根据ID查询关键字配置")
    @GetMapping("/{id}")
    public Result<AgKeywordConfig> getById(@PathVariable Integer id) {
        AgKeywordConfig entity = keywordConfigService.getById(id);
        if (entity == null) {
            return Result.error(404, "未找到该关键字");
        }
        return Result.ok(entity);
    }

    @Operation(summary = "查询所有关键字配置")
    @GetMapping("/list")
    public Result<List<AgKeywordConfig>> getAll(@RequestParam Integer websiteId) {
        List<AgKeywordConfig> list = keywordConfigService.getAllByWebsiteId(websiteId);
        return Result.ok(list);
    }

    @Operation(summary = "新增关键字配置")
    @PostMapping
    public Result<Void> save(@RequestBody AgKeywordConfig entity) {
        keywordConfigService.create(entity);
        return Result.ok();
    }

    @Operation(summary = "更新关键字配置")
    @PutMapping
    public Result<Void> update(@RequestBody AgKeywordConfig entity) {
        if (entity.getId() == null) {
            return Result.error(400, "缺少ID");
        }
        keywordConfigService.update(entity);
        return Result.ok();
    }

    @Operation(summary = "删除关键字配置")
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Integer id) {
        keywordConfigService.delete(id);
        return Result.ok();
    }
}
